package com.ice.app.system.organ.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.iceframework.core.entity.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ice.app.system.entity.Organ;
import com.ice.app.system.organ.service.OrganService;
import com.ice.app.system.organ.vo.OrganTreeVo;
import com.iceframework.core.base.BaseController;
import com.iceframework.core.entity.Result;
import com.iceframework.core.utils.WebUtils;

/**
 * 
 * 控制层： 组织机构管理
 * 
 * @author Mr.Palo
 * @email 442803840@qq.com
 * 
 */
@RestController
@RequestMapping("/sys/organ")
public class OrganController extends BaseController {

	@Autowired
	private OrganService organService;

	/**
	 * 获取数据
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/get", method = RequestMethod.GET)
	public Result getOrgan(@RequestParam(value = "key") String key, HttpServletRequest request) {
		Organ organ = organService.getOrgan(key);
		return Result.success().add("organ", organ);
	}

	/**
	 * 首页加载数据
	 * 
	 * @param pn
	 * @param organ
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/index/data", method = RequestMethod.POST)
	@ResponseBody
	public Result organIndexData(Organ organ, Page<Organ> page, HttpServletRequest request) {
		PageHelper.startPage(page.getPageNum(), page.getPageSize(), page.getOrderBy());
		List<Organ> organs = organService.getOrgans(organ);
		PageInfo<Organ> data = new PageInfo<Organ>(organs, page.getPageSize());
		return Result.success().add("data", data);
	}

	/**
	 * 保存实体
	 * 
	 * @param organ
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public Result saveOrgan(Organ organ) throws Exception {
		if (organService.saveOrgan(organ)) {
			return Result.saveSuccess();
		} else {
			return Result.saveFail();
		}
	}

	/**
	 * 删除实体
	 * 
	 * @param keys
	 * @return
	 */
	@RequestMapping(value = "/del", method = RequestMethod.POST)
	@ResponseBody
	public Result delOrgan(String keys) {
		if (organService.deleteOrgans(keys)) {
			return Result.delSuccess();
		} else {
			return Result.delFail();
		}
	}

	/**
	 * 获取机构下拉树
	 * 
	 * @return
	 */
	@RequestMapping(value = "/tree", method = RequestMethod.GET)
	public List<OrganTreeVo> getAllTree() {
		return organService.getOrganTreeSelect("", "0");
	}

	/**
	 * 获取机构下拉树
	 * 
	 * @return
	 */
	@RequestMapping(value = "/my/tree", method = RequestMethod.GET)
	public Result getMyTree() {
		return Result.success().add("data", organService.getOrganTreeSelect(WebUtils.getUserId(), "0"));
	}

}